package com.treasure.mall.biz.repository.withdraw;

import com.treasure.mall.biz.base.JpaSearchableRepository;
import com.treasure.mall.biz.entity.withdraw.WithdrawPO;
import com.treasure.mall.zbq.common.ienum.withdraw.WithdrawEnums;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author zoul
 * @version 1.0
 * @date 2020/6/14 15:34
 * @Description
 */
@Repository
public interface WithdrawRepository extends JpaSearchableRepository<WithdrawPO, Long> {

    /**
     * 修改提现状态
     *
     * @param withdrawStatus 状态
     * @param reason         原因（成功直接传null）
     * @param applyId        提现主键Id
     */
    @Query("update WithdrawPO draw set draw.withdrawStatus=?1,draw.refuseReason=?2 where draw.applyId=?3")
    @Modifying
    @Transactional(rollbackFor = RuntimeException.class)
    void updateWithdrawStatus(WithdrawEnums.WithdrawStatus withdrawStatus, String reason, Long applyId);

    /**
     * 获取该用户申请了多少次提现
     *
     * @param withdrawType   提现类型
     * @param userId         用户Id
     * @param beginTime      起始时间
     * @param endTime        截止时间
     * @param withdrawStatus 状态
     * @return
     */
    @Query("select count(draw.applyId) from WithdrawPO draw where draw.userPo.userId=?2 and draw.withdrawType=?1 and draw.createTime>=?3 and draw.createTime<?4 and draw.withdrawStatus in ?5")
    int countWithdrawNum(WithdrawEnums.WithdrawType withdrawType, long userId, LocalDateTime beginTime, LocalDateTime endTime, List<WithdrawEnums.WithdrawStatus> withdrawStatus);


    /**
     * 获取所有提现银行卡号为空的数据
     *
     * @return
     */
    List<WithdrawPO> findAllByBankNoIsNull();
}

